package br.com.colegio.dao.storage;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import br.com.colegio.dao.ConfiguracaoDAO;
import br.com.colegio.vo.Configuracao;

public class ConfiguracaoData
{
	private static ConfiguracaoData instance;
	private Map<String, String> data = new HashMap<String, String>(); 

	private ConfiguracaoData()
	{
	}

	public static ConfiguracaoData getInstance()
	{
		if (instance == null)
			instance = new ConfiguracaoData();

		return instance;
	}

	private Map<String, String> getData()
	{
		if (data.size() == 0)
		{
			Collection<Object> r = ConfiguracaoDAO.list();

			if (r != null)
				for (Object o : r)
				{
					Configuracao c = (Configuracao)o;
					data.put(c.getDescricao(), c.getValor());
				}
		}

		return data;
	}

	public String get(String descricao)
	{
		return getData().get(descricao);
	}

	public void atualizaData(Configuracao c)
	{
		getData().put(c.getDescricao(), c.getValor());
	}

	public Configuracao save(Configuracao c) throws Exception
	{
		ConfiguracaoDAO.save(c);
		atualizaData(c);
		return c;
	}
}